<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class OwnserviceControllerBranch extends JController
{
	/**
	 * Controller construct
	 *
	*/
	function __construct(){
		parent::__construct();
	}

	/**
	 * Method to get detail branch
	 *
	 * @return void
	 * @since 1.0
	*/
	function detail() {

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25){
			$canView	= Ownservice::checkAccess('branch', 'view', 'branch', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('branch', 'html');

		// Get/Create the model
		$model = & $this->getModel('Branch');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('default');

		// Display the view
		$view->display();
	}

	/**
	 * Method to get form  edit branch
	 *
	 * @return void
	 * @since 1.0
	 */
	function edit(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25) {
			$canEdit	= Ownservice::checkAccess('branch', 'edit', 'branch', 'all');
			if ( !$canEdit ) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('branch', 'html');

		// Get/Create the model
		$model = & $this->getModel('Branch');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();

	}

	/**
	* Method to add an branch
	*
	* @access	public
	* @since	1.0
	*/
	function add() {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25) {
			$canAdd		= Ownservice::checkAccess('branch', 'add', 'branch', 'all');
			if (!$canAdd) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('branch', 'html');

		// Get/Create the model
		$model = & $this->getModel('Branch');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();
	}

	/**
	 * Logic to save branch
	 * 
	 * @access public
	 * @return void
	*/
	function save(){

		//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$user = &  JFactory::getUser();
		$app = & JFactory::getApplication();

		// Get vars
		$post = JRequest::get( 'post' );

		$post['advertising']	= JRequest::getVar( 'advertising', '', 'post', 'string', JREQUEST_ALLOWRAW );

		$model = $this->getModel('Branch');

		$type = '';

		if($model->store($post)){
			$msg = JText::_('OWNSERVICE_BRANCH_SAVED');
		} else {
			$type = 'error';
			$msg = JText::_('OWNSERVICE_ERROR_SAVING_BRANCH');
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=branches');
		
		$this->setRedirect($link, $msg, $type);
	}

	function delete(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$user = & JFactory::getUser();
		$app = & JFactory::getApplication();

		if( $user->gid <25 ) {
			$canDelete		= Ownservice::checkAccess('branch', 'delete', 'branch', 'all');
			if (!$canDelete) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//get var
		$id = JRequest::getVar('id',0, 'post', 'int');

		$model=& $this->getModel('Branches');

		if ($model->delete($id)) {
			$msg = JText::_( 'OWNSERVICE_BRANCH_DELETED' );
		} else {
			$msg = JText::_( 'OWNSERVICE_BRANCH_FAILED' );
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=branches');

		$this->setRedirect( $link, $msg );
	}

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel() {
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$app = & JFactory::getApplication();

		$link = JRoute::_('index.php?option=com_ownservice&view=branches');

		$this->setRedirect( $link );
	}
}